package com.hyperz;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.CheckBox;

public class Main extends Activity {
	//private final static String LOG = "Main"; 
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		final Button button = (Button) findViewById(R.id.btn_connect);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (isValidInput()) {
                	Bundle extras = new Bundle();
                	Intent myIntent = new Intent(getApplicationContext(), RepoTreeViewer.class);
                	
                	populateRepositoryDetails(extras);
                	populateProxyDetails(extras);
                	
                	myIntent.putExtras(extras);
                	
                	startActivity(myIntent);
                }
            }
        });
	}
	
	private void populateRepositoryDetails(Bundle bundle) {
		
		bundle.putString(this.getPackageName() + ".repo_url", getTextValue(R.id.repository_url));
		bundle.putString(this.getPackageName() + ".repo_path", "trunk");
		bundle.putString(this.getPackageName() + ".repo_uid", getTextValue(R.id.repository_uid));
		bundle.putString(this.getPackageName() + ".repo_pwd", getTextValue(R.id.repository_pwd));
		
	}
	
	private void populateProxyDetails(Bundle bundle) {
		
		bundle.putString(this.getPackageName() + ".proxy_host", getTextValue(R.id.proxy_host));
		bundle.putInt(this.getPackageName() + ".proxy_port", Integer.parseInt(getTextValue(R.id.proxy_port)));
		bundle.putString(this.getPackageName() + ".proxy_uid", getTextValue(R.id.proxy_uid));
		bundle.putString(this.getPackageName() + ".proxy_pwd", getTextValue(R.id.proxy_pwd));
		
	}
	
	private boolean isValidInput() {
		boolean rv = false;
		
		//validate that the repository URL, uid and pwd are populated
		if (getTextValue(R.id.repository_url).length() == 0) { 
			displayMessage("Repository URL cannot be blank");
		} else if (getTextValue(R.id.repository_uid).length() == 0) {
			displayMessage("Repository UserName cannot be blank");
		} else if ((getTextValue(R.id.repository_url).length() == 0)) {
			displayMessage("Repository Password cannot be blank");
		} else {
			if (((CheckBox) findViewById(R.id.use_proxy)).isChecked()) {
				if (getTextValue(R.id.proxy_host).length() == 0) { 
					displayMessage("Proxy Host cannot be blank");
				} else if (getTextValue(R.id.proxy_port).length() == 0) { 
					displayMessage("Proxy Port cannot be blank");
				} else if (getTextValue(R.id.proxy_pwd).length() == 0) { 
					displayMessage("Proxy Password cannot be blank");
				} else if (getTextValue(R.id.proxy_uid).length() == 0) { 
					displayMessage("Proxy UserName cannot be blank");
				} else {
					rv = true;
				}
			} else {
				rv = true;
			}
		}
		
		return rv;
	}
	
	private String getTextValue(int resId) {
		return ((EditText) findViewById(resId)).getText().toString();
	}
	
	private void displayMessage(String msg) {
		((TextView) findViewById(R.id.label_message)).setText(msg); 
	}
}